package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;

@Service
@Transactional
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {

    /**
     * 登录功能
     *
     * @param dto
     * @return
     */         //isNotEmpty 不为空  需要具体的值 空白字符也算" "  不严谨  所以选择第二个
    @Override  //isNotBlank 不为空 需要具体参数,不包括 空格 空白符 和null
    public ResponseResult Login(LoginDto dto) {
        /*if (StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())) {
            //根据用手机号查询用户信息
            ApUser apUser = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, dto.getPhone()));
            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户信息不存在");
            }
            //对比密码
            String salt = apUser.getSalt();
            String password = dto.getPassword();
            String pwg = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            if (!pwg.equals(apUser.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //返回数据  jwt  user
            String token = AppJwtUtil.getToken(apUser.getId().longValue());
            HashMap<String, Object> map = new HashMap<>();
            map.put("token", token);
            apUser.setSalt("");
            apUser.setPassword("");
            map.put("user", apUser);
            return ResponseResult.okResult(map);
        } else {
            //游客登录
            HashMap<String, Object> map = new HashMap<>();
            String token = AppJwtUtil.getToken(0l);
            map.put("token", token);
            return ResponseResult.okResult(map);
        }*/

        if (StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())) {
            //根据手机号获取用户
            ApUser user = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, dto.getPhone()));
            //判断用户是否存在
            if (user == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"请重新输入账号后再试");
            }
            //判断密码是否正确
            String salt = user.getSalt();
            String password = dto.getPassword();
            String s = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            if (!s.equals(user.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

            String token = AppJwtUtil.getToken(user.getId().longValue());
            HashMap<String, Object> map = new HashMap<>();
            map.put("token", token);
            user.setSalt("");
            user.setPassword("");
            map.put("user", user);
            return ResponseResult.okResult(map);
        }

        //如果为空   游客登陆
        else {
            HashMap<String, Object> map = new HashMap<>();
            String token = AppJwtUtil.getToken(0l);
            return ResponseResult.okResult(token);
        }
    }

}


